package com.healthtop.service.impl;

import com.healthtop.common.Specification;
import com.healthtop.dao.XsResourceDao;
import com.healthtop.dto.MenuDto;
import com.healthtop.dto.NavigationDto;
import com.healthtop.dto.ResourceDto;
import com.healthtop.model.XsResource;
import com.healthtop.model.XsRoleResource;
import com.healthtop.service.NavigationService;
import com.healthtop.service.XsRoleResourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by skie on 18-6-13.
 */
@Service
public class NavigationServiceImpl implements NavigationService {

    @Autowired
    private XsResourceDao resourceDao;

    @Autowired
    private XsRoleResourceService roleResourceService;

    @Autowired
    private DIYServiceImpl diyService;

    @Override
    public NavigationDto navigationById(NavigationDto navigationDto) {
        if(navigationDto.getMode()== Specification.OperateMode.UPDATE.index) {
            XsResource resource = resourceDao.getResourceById(navigationDto.getId());
            BeanUtils.copyProperties(resource, navigationDto);
        }
        return navigationDto;
    }

//    @Override
//    public List<XsResource> selectParentResource() {
//        List<XsResource> resources = resourceDao.selectParentResource();
//        return resources;
//    }
//
//    @Override
//    public NavigationDto operateNavigation(NavigationDto navigationDto) {
//        XsResource resource = new XsResource();
//        BeanUtils.copyProperties(navigationDto,resource);
//        if(navigationDto.getMode() == Specification.OperateMode.CREATE.index){
//            resource.setCreateTime(new Date());
//            resource.setUpdateTime(new Date());
//            resource.setStatus(Specification.NavigationStatus.INSERT.index);
//            resourceDao.insertSelective(resource);
//        }else if(navigationDto.getMode() == Specification.OperateMode.UPDATE.index){
//            resource.setUpdateTime(new Date());
//            resourceDao.update(resource);
//        }
//        return navigationById(navigationDto);
//    }

    @Override
    public void delete(Long id) {
        XsResource resource = resourceDao.getResourceById(id);
        resource.setStatus(Specification.NavigationStatus.LEAVE.index);
        resourceDao.update(resource);
    }

//    @Override
//    public List<MenuDto> getMenusByRoleId(long roleId) {
//        List<XsRoleResource> roleResources = roleResourceService.selectByRoleId(roleId);
//        List<Long> rrIds = new ArrayList<>();
//        for(XsRoleResource roleResource: roleResources) {
//            rrIds.add(roleResource.getResourceId());
//        }
//        List<MenuDto> menus = new ArrayList<>();
//        List<XsResource> list = resourceDao.selectParentResource();
//        for(XsResource parent : list){
//            MenuDto menuDto = new MenuDto();
//            menuDto.setName(parent.getResourceName());
//            menuDto.setId(parent.getId());
//            if(rrIds.contains(parent.getId())) {
//                menuDto.setChecked(true);
//            }
//            menuDto.setOpen(true);
//            menus.add(menuDto);
//
//            List<XsResource> subList = resourceDao.selectResourceByPid(parent.getId());
//            for(XsResource sub : subList){
//                MenuDto subMenuDto = new MenuDto();
//                subMenuDto.setName(sub.getResourceName());
//                subMenuDto.setId(sub.getId());
//                if(rrIds.contains(sub.getId())) {
//                    subMenuDto.setChecked(true);
//                }
//                subMenuDto.setIsParent("false");
//                menuDto.getChildren().add(subMenuDto);
//            }
//        }
//        return menus;
//    }

    @Override
    public List<MenuDto> getMenus(long uid) {
        List<MenuDto> menus = new ArrayList<>();
        List<ResourceDto> list = diyService.getParentResource(uid);
        for(ResourceDto parent : list){
            MenuDto menuDto = new MenuDto();
            menuDto.setMenuName(parent.getMenuName());
            menuDto.setPath(parent.getPath());
            menuDto.setIcon(parent.getIcon());
            menuDto.setMenuId(parent.getMenuId());
            menus.add(menuDto);

            List<ResourceDto> subList = diyService.getResourceByPid(parent.getMenuId(), uid);
            for(ResourceDto sub : subList){
                MenuDto subMenuDto = new MenuDto();
                subMenuDto.setMenuName(sub.getMenuName());
                subMenuDto.setPath(sub.getPath());
                subMenuDto.setIcon(sub.getIcon());
                subMenuDto.setMenuId(sub.getMenuId());
                menuDto.getChildren().add(subMenuDto);
            }
        }
        return menus;
    }

}
